﻿using System;
using System.IO;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKRevitDimensionText.OneKeyPlanDim.LevelDim;
using YJKRevitDimensionText.OneKeyPlanDim.Utility.XDataUtils;

namespace YJKRevitDimensionText.OneKeyPlanDim.Utility
{
	public class SizeDimData
	{
		public SizeDimData(Autodesk.Revit.DB.Document doc, Line line, ReferenceArray arRef)
		{
			this.m_line = line;
			this.m_arRef = arRef;
			this.m_doc = doc;
		}

		public Dimension CreateDim(bool bResetDimPos = true)
		{
			Dimension dimension = null;
			Transaction transaction = new Transaction(this.m_doc, "SizeDim");
			try
			{
				transaction.Start();
				if (SizeDimData.m_dimensionType == null)
				{
					dimension = this.m_doc.Create.NewDimension(this.m_doc.ActiveView, this.m_line, this.m_arRef);
				}
				else
				{
					dimension = this.m_doc.Create.NewDimension(this.m_doc.ActiveView, this.m_line, this.m_arRef, SizeDimData.m_dimensionType);
				}
				if (bResetDimPos)
				{
					RevitDimensionExtension.ResetDimTextPos(dimension);
				}
				ElementStringXDataUtil.WriteOneKeyPlaneDimMark(dimension);
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
			if (dimension != null)
			{
				KeyComponent.GetInstance().m_lstSizeDimension.Add(dimension);
			}
			return dimension;
		}

		public static DimensionType GetLevelDimFamilySymbol(Autodesk.Revit.DB.Document doc)
		{
			Transaction transaction = new Transaction(doc, "CreateDim");
			try
			{
				transaction.Start();
				string strFamilyFilePath = Path.Combine(Product.FamilyLocation, RevitVersion.Version, "OneKeyDim", "标高、坡道族、尺寸标注.rvt");
				SizeDimData.m_dimensionType = FindFamilySymbol.GetDimensionTypes(doc, strFamilyFilePath, SizeDimData.m_strFamilyName, SizeDimData.m_strFamilySymbolName);
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
			return SizeDimData.m_dimensionType;
		}

		private Document m_doc;

		public static DimensionType m_dimensionType = null;

		public static string m_strFamilyFilePath = "C:\\Users\\zk\\Desktop\\标高、坡道族、尺寸标注.rvt";

		public static string m_strFamilyName = "线性尺寸标注样式";

		public static string m_strFamilySymbolName = "HY-尺寸标注";

		private Line m_line;

		private ReferenceArray m_arRef;
	}
}
